<?php
declare (strict_types = 1);
namespace app\admin\model;

use think\Model;

class AdminLog extends Model
{

    /**
     * 读取日志标题
     * @return array
     */
    public function get_title($type)
    {
        $type_array = [
            'Conf/conf_submit' => '配置信息',
            'Menu/post_submit' => '菜单',
            'Menu/delete' => '菜单',
            'Rule/post_submit' => '节点',
            'Rule/delete' => '节点',
            'Role/post_submit' => '角色',
            'Role/delete' => '角色',
            'Admin/post_submit' => '管理员',
            'Admin/view' => '管理员',
            'Admin/delete' => '管理员',
            'Nav/post_submit' => '导航组',
            'Nav/delete' => '导航组',
            'Nav/nav_info_submit' => '导航',
            'Nav/nav_info_delete' => '导航',
            'Sitemap/post_submit' => '网站地图分类',
            'Sitemap/delete' => '网站地图分类',
            'Sitemap/sitemap_info' => '网站地图',
            'Sitemap/sitemap_info_submit' => '网站地图',
            'Sitemap/sitemap_info_delete' => '网站地图',
            'Slide/post_submit' => '轮播组',
            'Slide/delete' => '轮播组',
            'Slide/slide_info_submit' => '轮播图',
            'Slide/slide_info_delete' => '轮播图',
            'Keywords/post_submit' => 'SEO关键字',
            'Keywords/delete' => 'SEO关键字',
            'Search/delete' => '搜索关键字',
            'User/post_submit' => '用户',
            'User/view' => '用户',
            'User/delete' => '用户',
            'Article/cate_post_submit' => '文章分类',
            'Article/cate_delete' => '文章分类',
            'Article/post_submit' => '文章',
            'Article/delete' => '文章',
            'Api/upload' => '附件',
            'Login/login_submit' => '系统',
			'Consultation/post_submit' => '咨询',
            'Consultation/delete' => '咨询',
            'Dynamic/post_submit' => '动态',
            'Dynamic/delete' => '动态',
        ];
        if (!empty($type_array[$type])) {
            return $type_array[$type];
        } else {
            return '未知';
        }
    }
    /**
     * 读取日志模块
     * @return array
     */
    public function get_controller($controller, $fun)
    {
        $controller_array = ['Admin' => '系统用户', 'Article' => '文章', 'Conf' => '配置信息', 'Index' => '系统1', 'Keywords' => '关键字', 'Login' => '系统', 'Menu' => '菜单', 'Nav' => '导航分类', 'Role' => '角色', 'Rule' => '节点', 'Search' => '搜索词', 'Sitemap' => '网站地图', 'Slide' => '广告', 'User' => '用户', 'Api' => ''];
        if (!empty($controller_array[$controller])) {
            if ($fun == 'cate_post_submit') {
                return '文章分类';
            } else {
                return $controller_array[$controller];
            }
        } else {
            return '';
        }
    }

    public function get_log_list($param = [])
    {
        $where = array();
        if (!empty($param['no_delete'])) {
            $where['type'] = ['neq', 'delete']; //过滤删除操作
        }
        if (!empty($param['no_admin'])) {
            $where['uid'] = ['neq', 1]; //超级管理员删除操作
        }
        if (!empty($param['uid'])) {
            $where['uid'] = $param['uid']; //查询指定用户的操作
        }
        $where['status'] = 1;
        $rows = empty($param['limit']) ? get_config('app.pages') : $param['limit'];
        $content = \think\facade\Db::name('admin_log')
            ->field("id,uid,nickname,type,title,module,controller,function,param,create_time")
            ->order('create_time desc')
            ->where($where)
            ->paginate($rows, false, ['query' => $param]);

        $content->toArray();
        foreach ($content as $k => $v) {
            $data = $v;
            $param_array = json_decode($v['param'], true);
            $name = '';
            if (!empty($param_array['name'])) {
                $name = $param_array['name'];
            }
            if (!empty($param_array['title'])) {
                $name = $param_array['title'];
            }
            $data['content'] = $v['nickname'] . $v['title'] . '了' . $this->get_title($v['controller'] . '/' . $v['function']) . $name;
            $data['times'] = time_trans($v['create_time']);
            $content->offsetSet($k, $data);
        }
        return $content;
    }
}
